package com.ssm.web;

import com.ssm.Service.UserService;
import com.ssm.Service.changepasswordService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "changePasswordServlet",urlPatterns = "/changepass")
public class changePasswordServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username= (String) request.getSession().getAttribute("name");
        String oldpassword=request.getParameter("oldpassword");
        String newpassword=request.getParameter("newpassword");

//        System.out.println("username:"+username);
//        System.out.println("oldpassword:"+oldpassword);
//        System.out.println("newpassword:"+newpassword);

        UserService userService=new UserService();

        boolean register = userService.register(username, oldpassword);

//        System.out.println("register:"+register);
        if(register){
            changepasswordService changepasswordService=new changepasswordService();
            boolean change = changepasswordService.change(username, newpassword);
            if(change){
                request.getSession().removeAttribute("name");
                response.sendRedirect("/");
            }else{
                response.sendRedirect("/jsp/404.jsp");
            }
        }else{
            response.sendRedirect("/jsp/404.jsp");
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
